package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edu.domain.vo.EduVideoVo;
import com.ruoyi.edu.domain.bo.EduVideoBo;
import com.ruoyi.edu.service.IEduVideoService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程视频
 *
 * @author ruoyi
 * @date 2022-08-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/edu/video")
public class EduVideoController extends BaseController {

    private final IEduVideoService iEduVideoService;

    /**
     * 查询课程视频列表
     */
    @SaCheckPermission("edu:video:list")
    @GetMapping("/list")
    public TableDataInfo<EduVideoVo> list(EduVideoBo bo, PageQuery pageQuery) {
        return iEduVideoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出课程视频列表
     */
    @SaCheckPermission("edu:video:export")
    @Log(title = "课程视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EduVideoBo bo, HttpServletResponse response) {
        List<EduVideoVo> list = iEduVideoService.queryList(bo);
        ExcelUtil.exportExcel(list, "课程视频", EduVideoVo.class, response);
    }

    /**
     * 获取课程视频详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("edu:video:query")
    @GetMapping("/{id}")
    public R<EduVideoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iEduVideoService.queryById(id));
    }

    /**
     * 新增课程视频
     */
    @SaCheckPermission("edu:video:add")
    @Log(title = "课程视频", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EduVideoBo bo) {

        return toAjax(iEduVideoService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改课程视频
     */
    @SaCheckPermission("edu:video:edit")
    @Log(title = "课程视频", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EduVideoBo bo) {
        return toAjax(iEduVideoService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除课程视频
     *
     * @param ids 主键串
     */
    @SaCheckPermission("edu:video:remove")
    @Log(title = "课程视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iEduVideoService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
